//登录 登出 获取信息
import Cookies from "js-cookie"
// import { login, logout, getInfo } from '@/api/user'
//获取token 设置token 销毁token
import { getToken, setToken, removeToken } from '@/utils/auth'
//重置路由
import { resetRouter } from '@/router'

//user模块信息的储存
// const getDefaultState = () => {
//   return {
//     token: getToken(),
//     name: '',
//     avatar: '',
//     introduction:'',
//     age:''
//   }
// }

const state = {
  name: '',
  userInfo:JSON.parse(localStorage.getItem("userInfo"))|| {}
}

const mutations = {
  //重置state

  //赋值name
  SET_NAME: (state, name) => {
    state.name = name
  },
  setUserInfo:(state,userInfo)=>{
    console.log("userInfo",userInfo)
    state.userInfo = userInfo
  },
  resetUserInfo: (state) => {
    //后面对象属性添加给前面state
    // Object.assign(state, getDefaultState())
    state.name = "";
    state.userInfo = {}
    localStorage.clear()
    sessionStorage.clear()
    Cookies.remove("token")
    Cookies.remove("userName")
    Cookies.remove("userpwd")
    Cookies.remove("userRId")
    Cookies.remove("user")
    Cookies.remove("realusername")
  },
}
const actions = {
  logout({commit,state}){
    return new Promise(((resolve, reject) => {
      commit('resetUserInfo')
      resolve()
    }))
  }
}



export default {
  namespaced: true,
  state,
  mutations,
  actions
}

